package com.lcb.rabbitmq.callback;

import com.lcb.rabbitmq.entity.MsgLog;
import com.lcb.rabbitmq.mapper.MsgLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/****
 * @Author:刘长柏
 * @Date: 2021-01-27 10:45
 * @Description: 消息到Exchange回调确认
 *****/
@Component
@Slf4j
public class MyConfirmCallback implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private MsgLogMapper msgLogMapper;

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("进入Exchange回调");
        String msgId = correlationData.getId();
        MsgLog msgLog = msgLogMapper.selectByPrimaryKey(msgId);
        Integer status = msgLog.getStatus();
        if (ack) {
            log.info("消息成功发送到Exchange");
            if (status != 2) {
                msgLog.setStatus(1);
            }
        } else {
            msgLog.setStatus(2);
            log.info("消息发送到Exchange失败, {}, cause: {}", correlationData, cause);
        }

        msgLog.setUpdateTime(new Date());
        msgLogMapper.updateByPrimaryKeySelective(msgLog);

    }
}